<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!DOCTYPE html><html xmlns="http://www.w3.org/1999/xhtml" xmlns:epub="http://www.idpf.org/2007/ops" xmlns:m="http://www.w3.org/1998/Math/MathML" xmlns:pls="http://www.w3.org/2005/01/pronunciation-lexicon" xmlns:ssml="http://www.w3.org/2001/10/synthesis" xmlns:svg="http://www.w3.org/2000/svg">
  <head>
    <title>Extended Formal Arguments List</title>
    <link rel="stylesheet" type="text/css" href="docbook-epub.css"/>
    <link rel="stylesheet" type="text/css" href="kawa.css"/>
    <script src="kawa-ebook.js" type="text/javascript"/>
    <meta name="generator" content="DocBook XSL-NS Stylesheets V1.79.1"/>
    <link rel="prev" href="Generic-procedures.xhtml" title="Generic (dynamically overloaded) procedures"/>
    <link rel="next" href="Partial-application.xhtml" title="Partial application"/>
  </head>
  <body>
    <header/>
    <section class="sect1" title="Extended Formal Arguments List" epub:type="subchapter" id="Extended-formals">
      <div class="titlepage">
        <div>
          <div>
            <h2 class="title" style="clear: both">Extended Formal Arguments List</h2>
          </div>
        </div>
      </div>
      <p>The formal arguments list of a lambda expression has some
extensions over standard Scheme:
Kawa borrows the extended formal argument list of DSSSL,
and Kawa allows you to declare the type of the parameter.
More generally, you can use <a class="link" href="Variables-and-Patterns.xhtml" title="Variables and Patterns">patterns</a>.
</p>
      <div class="literallayout">
        <p><a id="idm139667877350272" class="indexterm"/><span id="meta-lambda-expression"/><em class="replaceable"><code>lambda-expression</code></em> <code class="literal">::=</code> <code class="literal"><span class="bold"><strong>(lambda</strong></span></code> <a class="link" href="Extended-formals.xhtml#meta-formals"><em class="replaceable"><code>formals</code></em></a> <a class="link" href="Defining-new-classes.xhtml#meta-option-pair"><em class="replaceable"><code>option-pair</code></em></a><sup>*</sup> <a class="link" href="Extended-formals.xhtml#meta-opt-return-type"><em class="replaceable"><code>opt-return-type</code></em></a> <a class="link" href="Bodies.xhtml#meta-body"><em class="replaceable"><code>body</code></em></a><code class="literal"><span class="bold"><strong>)</strong></span></code><br/>
<a id="idm139667877343296" class="indexterm"/><span id="meta-return-type"/><em class="replaceable"><code>return-type</code></em> <code class="literal">::=</code> <a class="link" href="Types.xhtml#meta-type"><em class="replaceable"><code>type</code></em></a><br/>
<a id="idm139667877340496" class="indexterm"/><span id="meta-opt-return-type"/><em class="replaceable"><code>opt-return-type</code></em> <code class="literal">::=</code> [<code class="literal"><span class="bold"><strong>::</strong></span></code> <a class="link" href="Types.xhtml#meta-type"><em class="replaceable"><code>type</code></em></a>]<br/>
</p>
      </div>
      <p>where
</p>
      <div class="literallayout">
        <p><a id="idm139667877336496" class="indexterm"/><span id="meta-formals"/><em class="replaceable"><code>formals</code></em> <code class="literal">::=</code> <code class="literal"><span class="bold"><strong>(</strong></span></code><a class="link" href="Extended-formals.xhtml#meta-formal-arguments"><em class="replaceable"><code>formal-arguments</code></em></a><code class="literal"><span class="bold"><strong>)</strong></span></code> | <a class="link" href="Extended-formals.xhtml#meta-rest-arg"><em class="replaceable"><code>rest-arg</code></em></a><br/>
</p>
      </div>
      <p>You can of course also use the extended format in a <a class="link" href="Definitions.xhtml" title="Definitions"><code class="literal">define</code></a>:
</p>
      <pre class="screen"><span class="bold"><strong>(define (</strong></span><em class="replaceable"><code>name</code></em> <em class="replaceable"><code>formal-arguments</code></em><span class="bold"><strong>)</strong></span> [<em class="replaceable"><code>rtype</code></em>] <em class="replaceable"><code>body</code></em><span class="bold"><strong>)</strong></span>
</pre>
      <div class="literallayout">
        <p><a id="idm139667877326896" class="indexterm"/><span id="meta-formal-arguments"/><em class="replaceable"><code>formal-arguments</code></em> <code class="literal">::=</code><br/>
    <a class="link" href="Extended-formals.xhtml#meta-req-opt-args"><em class="replaceable"><code>req-opt-args</code></em></a> (<a class="link" href="Extended-formals.xhtml#meta-rest-key-args"><em class="replaceable"><code>rest-key-args</code></em></a> | <code class="literal"><span class="bold"><strong>.</strong></span></code> <a class="link" href="Extended-formals.xhtml#meta-rest-arg"><em class="replaceable"><code>rest-arg</code></em></a>)<br/>
</p>
      </div>
      <div class="literallayout">
        <p><a id="idm139667877321552" class="indexterm"/><span id="meta-req-opt-args"/><em class="replaceable"><code>req-opt-args</code></em> <code class="literal">::=</code> (<a class="link" href="Extended-formals.xhtml#meta-required-arg"><em class="replaceable"><code>required-arg</code></em></a>|<a class="link" href="Variables-and-Patterns.xhtml#meta-guard"><em class="replaceable"><code>guard</code></em></a>)<sup>*</sup> [<code class="literal"><span class="bold"><strong>#!optional</strong></span></code> <a class="link" href="Extended-formals.xhtml#meta-optional-arg"><em class="replaceable"><code>optional-arg</code></em></a> ...]<br/>
<a id="idm139667877315888" class="indexterm"/><span id="meta-rest-key-args"/><em class="replaceable"><code>rest-key-args</code></em> <code class="literal">::=</code> [<code class="literal"><span class="bold"><strong>#!rest</strong></span></code> <a class="link" href="Extended-formals.xhtml#meta-rest-arg"><em class="replaceable"><code>rest-arg</code></em></a>] [<code class="literal"><span class="bold"><strong>#!key</strong></span></code> <a class="link" href="Extended-formals.xhtml#meta-key-arg"><em class="replaceable"><code>key-arg</code></em></a> ...]<br/>
  | [<code class="literal"><span class="bold"><strong>#!key</strong></span></code> <a class="link" href="Extended-formals.xhtml#meta-key-arg"><em class="replaceable"><code>key-arg</code></em></a> ...] [<code class="literal"><span class="bold"><strong>#!rest</strong></span></code> <a class="link" href="Extended-formals.xhtml#meta-rest-arg"><em class="replaceable"><code>rest-arg</code></em></a>]<br/>
<a id="idm139667877307312" class="indexterm"/><span id="meta-required-arg"/><em class="replaceable"><code>required-arg</code></em> <code class="literal">::=</code> <a class="link" href="Variables-and-Patterns.xhtml#meta-pattern"><em class="replaceable"><code>pattern</code></em></a> [<code class="literal"><span class="bold"><strong>::</strong></span></code> <a class="link" href="Types.xhtml#meta-type"><em class="replaceable"><code>type</code></em></a>]<br/>
  | <code class="literal"><span class="bold"><strong>(</strong></span></code> <a class="link" href="Variables-and-Patterns.xhtml#meta-pattern"><em class="replaceable"><code>pattern</code></em></a> <code class="literal"><span class="bold"><strong>::</strong></span></code> <a class="link" href="Types.xhtml#meta-type"><em class="replaceable"><code>type</code></em></a><code class="literal"><span class="bold"><strong>)</strong></span></code><br/>
<a id="idm139667877299024" class="indexterm"/><span id="meta-optional-arg"/><em class="replaceable"><code>optional-arg</code></em> <code class="literal">::=</code> <em class="replaceable"><code>variable</code></em> [<code class="literal"><span class="bold"><strong>::</strong></span></code> <a class="link" href="Types.xhtml#meta-type"><em class="replaceable"><code>type</code></em></a>]<br/>
  | <code class="literal"><span class="bold"><strong>(</strong></span></code> <a class="link" href="Variables-and-Patterns.xhtml#meta-pattern"><em class="replaceable"><code>pattern</code></em></a> [<code class="literal"><span class="bold"><strong>::</strong></span></code> <a class="link" href="Types.xhtml#meta-type"><em class="replaceable"><code>type</code></em></a>] [<em class="replaceable"><code>initializer</code></em> [<a class="link" href="Extended-formals.xhtml#meta-supplied-var"><em class="replaceable"><code>supplied-var</code></em></a>]]<code class="literal"><span class="bold"><strong>)</strong></span></code><br/>
<a id="idm139667877289776" class="indexterm"/><span id="meta-supplied-var"/><em class="replaceable"><code>supplied-var</code></em> <code class="literal">::=</code> <em class="replaceable"><code>variable</code></em><br/>
<a id="idm139667877287376" class="indexterm"/><span id="meta-key-arg"/><em class="replaceable"><code>key-arg</code></em> <code class="literal">::=</code> <em class="replaceable"><code>variable</code></em> [<code class="literal"><span class="bold"><strong>::</strong></span></code> <a class="link" href="Types.xhtml#meta-type"><em class="replaceable"><code>type</code></em></a>]<br/>
    | <code class="literal"><span class="bold"><strong>(</strong></span></code> <em class="replaceable"><code>variable</code></em> [<code class="literal"><span class="bold"><strong>::</strong></span></code> <a class="link" href="Types.xhtml#meta-type"><em class="replaceable"><code>type</code></em></a>] [<em class="replaceable"><code>initializer</code></em> [<a class="link" href="Extended-formals.xhtml#meta-supplied-var"><em class="replaceable"><code>supplied-var</code></em></a>]] <code class="literal"><span class="bold"><strong>)</strong></span></code><br/>
<a id="idm139667877278528" class="indexterm"/><span id="meta-rest-arg"/><em class="replaceable"><code>rest-arg</code></em> <code class="literal">::=</code> <em class="replaceable"><code>variable</code></em><br/>
</p>
      </div>
      <p>When the procedure is applied to a list of actual arguments, the formal and
actual arguments are processed from left to right as follows:
</p>
      <div class="itemizedlist" epub:type="list">
        <ul class="itemizedlist" style="list-style-type: disc; ">
          <li class="listitem" epub:type="list-item">
            <p>The <em class="replaceable"><code>required-arg</code></em>s are matched against actual (pre-keyword) arguments
in order, starting with the first actual argument.
A <em class="replaceable"><code>guard</code></em> is evaluated when it appears:
If it evaluates to false, then matching fails.
It shall be an error if there are fewer pre-keyword
arguments then there are <em class="replaceable"><code>req-arg</code></em>s.
</p>
          </li>
          <li class="listitem" epub:type="list-item">
            <p>Next the <em class="replaceable"><code>optional-arg</code></em>s are bound to remaining pre-keyword arguments.
If there are fewer remaining pre-keyword arguments than there are
<em class="replaceable"><code>optional-arg</code></em>s, then the remaining <em class="replaceable"><code>variable</code></em>s are bound
to the corresponding <em class="replaceable"><code>initializer</code></em>.
If no <em class="replaceable"><code>initializer</code></em> was specified, it defaults to <code class="literal">#f</code>.
(TODO: If a <em class="replaceable"><code>type</code></em> is specified the default for <em class="replaceable"><code>initializer</code></em>
is the default value of the <em class="replaceable"><code>type</code></em>.)
The <em class="replaceable"><code>initializer</code></em> is evaluated in an
environment in which all the previous formal parameters have been bound.
If a <em class="replaceable"><code>supplied-var</code></em> is specified, it has type boolean,
and is set to true if there was an actual corresponding argument,
and false if the initializer was evaluated.
</p>
          </li>
          <li class="listitem" epub:type="list-item">
            <p>If there is a <em class="replaceable"><code>rest-arg</code></em>, it is bound to a list of all the
remaining actual arguments.  These remaining actual arguments are also
eligible to be bound to keyword arguments.   If there is no
<em class="replaceable"><code>rest-arg</code></em> and there are no <em class="replaceable"><code>key-arg</code></em>s, then it shall
be an error if there are any remaining actual arguments.
</p>
          </li>
          <li class="listitem" epub:type="list-item">
            <p>If <code class="literal">#!key</code> was specified, then there shall be an even number of
remaining actual arguments.  These are interpreted as a series of pairs,
where the first member of each pair is a keyword specifying the argument name,
and the second is the corresponding value.  It shall be an error if the first
member of a pair is not a keyword.  It shall be an error if the argument name
is not the same as a variable in a <em class="replaceable"><code>key-arg</code></em>s, unless there
is a <em class="replaceable"><code>rest-arg</code></em>.  If the same argument name occurs more than once
in the list of actual arguments, then the first value is used.
If there is no actual argument for a particular <em class="replaceable"><code>key-arg</code></em>,
then the variable is bound
to the corresponding <em class="replaceable"><code>initializer</code></em>, if one was specified, and
otherwise to <code class="literal">#f</code>.  The <em class="replaceable"><code>initializer</code></em> is evaluated in an
environment in which all the previous formal parameters have been bound.
</p>
          </li>
        </ul>
      </div>
      <p>If a <em class="replaceable"><code>type</code></em> is specified, the corresponding actual argument (or
the <em class="replaceable"><code>initializer</code></em> default value) is coerced to the specified <em class="replaceable"><code>type</code></em>.
In the function body, the parameter has the specified type.
</p>
      <p>If <em class="replaceable"><code>rtype</code></em> (the first form of the function body) is an unbound
identifier of the form <code class="literal">&lt;TYPE&gt;</code> (that is the first character
is ‘<code class="literal">&lt;</code>’ and the last is ‘<code class="literal">&gt;</code>’), then that specifies the
function’s return type.  It is syntactic sugar for
<code class="literal">(as &lt;TYPE&gt; (begin BODY))</code>.
</p>
      <p>You can set the <a class="link" href="Procedure-properties.xhtml" title="Procedure properties">properties</a>
of the resulting procedure using an <em class="replaceable"><code>option-pair</code></em>.  For example,
to set the <code class="literal">setter</code> property of a procedure
to <code class="literal">my-set-car</code> do the following:
</p>
      <pre class="screen">(define my-car
  (lambda (arg) setter: my-set-car (primitive-car arg)))
</pre>
    </section>
    <footer>
      <div class="navfooter">
        <p>
          Up: <a accesskey="u" href="Procedures.xhtml">Procedures</a></p>
        <p>
        Previous: <a accesskey="p" href="Generic-procedures.xhtml">Generic (dynamically overloaded) procedures</a></p>
        <p>
        Next: <a accesskey="n" href="Partial-application.xhtml">Partial application</a></p>
      </div>
    </footer>
  </body>
</html>
